package service

import (
	"duoke/internal/dao"
	"duoke/internal/model/entity"
	"duoke/model"
	"duoke/tables"
	"duoke/utils/orm"
	"errors"
	"fmt"
	"github.com/gin-gonic/gin"
)

var CourseShare = new(courseShareService)

type courseShareService struct {
	TablesName string
}

func (s *courseShareService) Info(c *gin.Context, id int, field string) (*tables.CourseShare, error) {
	userR, _ := Request.User(c)
	user := new(tables.CourseShare)
	db := orm.Db.Where("from_id=? and user_id=? and state = 1", id, userR.Id)
	_, err := db.Cols(field).Get(user)
	if err != nil {
		return nil, err
	}
	return user, nil
}
func (s *courseShareService) Save(c *gin.Context, id int, share_type int) error {
	user, err := Request.User(c)
	if err != nil {
		return err
	}
	var cv entity.CourseList
	one, err := dao.CourseList.Ctx(c).Where("id=?", id).One()
	if err != nil {
		return err
	}
	if one.IsEmpty() {
		return errors.New("该视频栏目不存在")
	}
	err = one.Struct(&cv)
	if err != nil {
		return err
	}
	var cs entity.CourseShare
	_ = dao.CourseShare.Ctx(c).Where("from_id=? and user_id=? and type=?",
		id, user.Id, share_type).Scan(&cs)
	state := 1 //用于判断用户是否取消的按钮
	if share_type == 1 {
		if cs.Id > 0 && cs.State == 1 {
			state = 0
		}
	}
	courseShare := entity.CourseShare{
		FromId:   cv.Id,
		Type:     share_type,
		UserId:   user.Id,
		Acid:     user.Acid,
		State:    state,
		SubMchId: cv.SubMchId,
	}
	if cs.Id > 0 {
		courseShare.Id = cs.Id
	}
	_, err = dao.CourseShare.Ctx(c).Save(&courseShare)
	if err != nil {
		return err
	}
	// 转发可以多次
	if share_type == 1 {
		if cs.Id > 0 && cs.State == 1 {
			// 如果存在 再次点击则取消点赞
			_, _ = dao.CourseList.Ctx(c).Where("id", id).Decrement("collect", 1)
		} else {
			_, _ = dao.CourseList.Ctx(c).Where("id", id).Increment("collect", 1)
		}
	} else {
		if cs.Id == 0 && state == 1 {
			_, _ = dao.CourseList.Ctx(c).Where("id", id).Increment("shares", 1)
		}
	}
	return nil
}
func (s *courseShareService) List(c *gin.Context, req *model.CourseShareListReq) (*[]model.CourseShareListRsp,
	int64, error) {
	user, err := Request.User(c)
	if err != nil {
		return nil, 0, err
	}
	// 查询结果
	where := fmt.Sprintf("course_share.acid=%d and type=%d and user_id=%d and course_share."+
		"state = 1",
		user.Acid, req.ShareType, user.Id)
	list := make([]model.CourseJoinList, 0)
	session := tableCommon.QueryListWithPageV2(where, req.PageNo, req.PageSize)
	session.Join("LEFT", "course_list", "course_list.id = course_share.from_id").
		Cols("number_of_learners,course_list.id,course_list.pic,price,title,from_id")
	total, err := session.FindAndCount(&list)
	if err != nil {
		return nil, 0, err
	}
	rsps := make([]model.CourseShareListRsp, 0)
	for _, item := range list {
		rsps = append(rsps, model.CourseShareListRsp{
			CourseShare:      item.CourseShare,
			Pic:              item.CourseList.Pic,
			NumberOfLearners: item.NumberOfLearners,
			Price:            item.Price,
			Title:            item.Title,
			Id:               item.CourseList.Id,
		})
	}
	return &rsps, total, nil
}
